﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using ImageTools;

namespace System
{
    public static class SLExtensions
    {
        #region Xml helpers
        public static IEnumerable<XElement> GetElementsByTagName(this XDocument doc, string tagName, string nameSapce = "")
        {
            return doc.Elements().Where(x => 
                    (x.Name.LocalName.Equals(tagName, StringComparison.InvariantCultureIgnoreCase) && String.IsNullOrEmpty(nameSapce)) ||
                    (x.Name.LocalName.Equals(tagName) && x.Name.Namespace.ToString().Equals(nameSapce))
                    ).AsEnumerable();
        }

        public static void RemoveNamedItem(this IEnumerable<XAttribute> xattrs, string localName, string nameSpace)
        {
            var attr = xattrs.Where(x => x.Name.LocalName.Equals(localName) && x.Name.Namespace.Equals(nameSpace)).FirstOrDefault();
            if (attr != null)
                attr.Remove();
        }
        public static XAttribute GetNamedItem(this IEnumerable<XAttribute> xattrs, string localName, string nameSpace = "")
        {
            return xattrs.Where(x => x.Name.LocalName.Equals(localName) && (String.IsNullOrEmpty(nameSpace) || x.Name.Namespace.Equals(nameSpace))).FirstOrDefault();
        }
        public static XAttribute SetNamedItem(this IEnumerable<XAttribute> xattrs, XAttribute newAttr)
        {
            var oldAttr = xattrs.GetNamedItem(newAttr.Name.LocalName, newAttr.Name.Namespace.ToString());
            if (oldAttr != null)
            {
                oldAttr.Remove();
                xattrs.FirstOrDefault().Parent.Add(newAttr);
                return oldAttr;
            }
            xattrs.FirstOrDefault().Parent.Add(newAttr);
            return newAttr;

        }
        public static bool HasChildNodes(this XElement elem)
        {
            return elem.Attributes().FirstOrDefault() != null || elem.Elements().FirstOrDefault() != null;
        }
        public static bool HasChildElements(this XElement elem)
        {
            return elem.Elements().FirstOrDefault() != null;
        }
        public static XElement FirstElement(this XElement elem)
        {
            return elem.Elements().FirstOrDefault();
        }
        public static String LocalName(this XElement elem)
        {
            return elem.Name.LocalName;
        }
        public static XElement NextSibling(this XElement elem)
        {
            return (XElement)elem.ElementsAfterSelf().FirstOrDefault();
        }
        public static String LocalName(this XAttribute elem)
        {
            return elem.Name.LocalName;
        }
        public static XAttribute NextSibling(this XAttribute elem)
        {
            return (XAttribute)elem.NextAttribute;
        }
        #endregion
        #region Imaging Helpers
        public static void Save(this ExtendedImage img, Stream ms, ImageFormat format)
        {
            switch (format)
            {
                case ImageFormat.Bmp:
                    var bmp = new ImageTools.IO.Bmp.BmpEncoder();
                    bmp.Encode(img, ms);
                    break;
                case ImageFormat.Png:
                    var png = new ImageTools.IO.Png.PngEncoder();
                    png.Encode(img, ms);
                    break;
                default:
                    var jpg = new ImageTools.IO.Jpeg.JpegEncoder();
                    jpg.Encode(img, ms);
                    break;
            }
        }
        #endregion
        #region Various Generic Collection/Dictionary helpers
        public static Hashtable Clone(this Hashtable ht)
        {
            var result = new Hashtable();
            foreach (var item in ht)
                result.Add(item.Key, item.Value);
            return result;
        }
        #endregion
        #region drawing helpers
        public static void SetPixel(this ExtendedImage bmp, int x, int y, System.Windows.Media.Color color)
        {
            bmp[x, y] = color;
        }
        #endregion
    }
}
